package com.tools.tools;

import com.aliyun.oss.OSSClient;

import java.io.File;
import java.net.URL;
import java.util.Date;

/**
 * Created by jianshan on 2018/3/1.
 */
public class OSSAliUtils {

    private static final String endpoint="oss-cn-hangzhou.aliyuncs.com";
//    private static final String accessKeyId="LTbIw6J5Mas3danq";
//    private static final String accessKeySecret="ddnrBObSNs801cterEhGa8njeLqyt7";

    private static final String accessKeyId="";
    private static final String accessKeySecret="";


    private static OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

    public static String uploadFile(String bucketName, String fileName, File file){

        ossClient.putObject(bucketName, fileName, file);

        return getObjectUrl(bucketName,fileName,null);

    }


    public static String getObjectUrl(String bucketName, String key, Long time){

        if(time==null || time>0){
            time=3600l;
        }

        Date expiration = new Date(new Date().getTime() + time * 1000);

        URL url = ossClient.generatePresignedUrl(bucketName, key, expiration);

        if(url!=null){
            return url.toString();
        }

        return null;
    }

    public static void deleteObject(String bucketName, String key){
         ossClient.deleteObject(bucketName,key);
    }

    public static void main(String[] args) {

        OSSAliUtils utils=new OSSAliUtils();
        String result=utils.uploadFile("jianshan","钉钉.mp4",new File("/Users/jianshan/Downloads/dingding.mp4"));
        System.out.println(result);

//        utils.deleteObject("mengyuvideo","钉钉.mp4");

    }



}
